package com.mz.auth.web.controller;


import com.mz.auth.entity.Menu;
import com.mz.auth.service.MenuService;
import com.mz.auth.util.MzResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class MenuController {

    @Autowired
    private MenuService menuService;

    //跳转试卷列表页面
    @GetMapping("/menu/index")
    public String index(Model model){
        List<Menu> menus = menuService.findMenus();
        model.addAttribute("menus",menus);

        return "views/menu/menu_list";
    }

    /**
     * 添加一级菜单
     * @param menu
     */
    @PostMapping("/menu/addTopMenu")
    @ResponseBody
    public MzResult addTopMenu(@RequestBody Menu menu){
        menuService.addTopMenu(menu);
        return MzResult.ok();
    }
    /**
     * 保存子菜单
     * @param menu
     * @return
     */
    @PostMapping("/menu/addSubMenu")
    @ResponseBody//返回数据是json格式
    public MzResult addSubMenu(@RequestBody Menu menu){
        try {
            menuService.addSubMenu(menu);
            return MzResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return MzResult.error("保存失败");
        }
    }

    /**
     * 根据id 删除菜单
     * @param id
     * @return
     */
    @PostMapping("/menu/deleteMenu")
    @ResponseBody
    public MzResult deleteMenu(Long id){
        try {
            menuService.deleteMenu(id);
            return MzResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return MzResult.error(e.getMessage());
        }
    }

    /**
     * 修改菜单 思路一样的
     * @param menu
     * @return
     */
    @PostMapping("/menu/editMenu")
    @ResponseBody
    public MzResult editMenu(@RequestBody Menu menu){
        try {
            menuService.editMenu(menu);
            return MzResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return MzResult.error(e.getMessage());
        }
    }

}
















